package com.ouss.tabs;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

public class DynamicFragment extends Fragment {
	public static final String TAG = "Test";

	public OptionsTab tab;

	private ScrollView mScrollView;
	private LinearLayout mFormView;

	private boolean mDone;

	private static int sId = 0;

	DynamicFragment(OptionsTab tab) {
		setTab(tab);
	}

	public OptionsTab getTab() {
		return tab;
	}

	public void setTab(OptionsTab tab) {
		this.tab = tab;
	}

	private static int id() {
		return sId++;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.d(TAG, "onAttach(): activity = " + activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate(): savedInstanceState = " + savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView(): container = " + container
				+ "savedInstanceState = " + savedInstanceState);

		if (mScrollView == null) {
			// normally inflate the view hierarchy
			mScrollView = (ScrollView) inflater.inflate(R.layout.fragment,
					container, false);
			mFormView = (LinearLayout) mScrollView.findViewById(R.id.form);

		} else {
			// mScrollView is still attached to the previous view hierarchy
			// we need to remove it and re-attach it to the current one
			ViewGroup parent = (ViewGroup) mScrollView.getParent();
			parent.removeView(mScrollView);
		}
		return mScrollView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "onActivityCreated(): savedInstanceState = "
				+ savedInstanceState);

		Handler mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				List<String> list = (List<String>) msg.obj;
				buildForm(list , tab);
			}
		};

		parseSite parse = new parseSite();
		parse.setResultat(tab.resultat);
		parse.setPromogoal(tab.promogoal);
		parse.setReturnValue(true);
		parse.setListView(null);
		parse.setHandler(mHandler);
		parse.setContext(getActivity());
		parse.setPd(ProgressDialog.show(getActivity(), "Working...",
				"request to server", true, false));
		parse.execute(tab.url);

	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView()");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy()");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach()");
	}

	private void buildForm(List<String> list, OptionsTab tab) {
		// if the view hierarchy was already build, skip this
		if (mDone)
			return;

		addFormFieldText(list.get(0), InputType.TYPE_CLASS_TEXT);

		for (int i = 1; i < list.size(); i++) {
			addFormFieldLabelSpinner(list.get(i), InputType.TYPE_MASK_FLAGS, tab.promogoal);
		}
		addFormFieldButon("Save");
		// the view hierarchy is now complete
		mDone = true;
	}

	private void addFormFieldButon(String label) {
		Button buyButton = new Button(getActivity());
		buyButton.setText(label);
		buyButton.setLayoutParams(new LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		mFormView.addView(buyButton);
	}
	
	private void addFormFieldText(String label, int type) {
		TextView tvLabel = new TextView(getActivity());
		tvLabel.setLayoutParams(getDefaultParams(true));
		tvLabel.setText(label);
		mFormView.addView(tvLabel);

	}
	
	private void addFormFieldLabelSpinner(String label, int type, boolean spinnerPromoGoal) {
		TextView tvLabel = new TextView(getActivity());
		tvLabel.setLayoutParams(getDefaultParams(true));
		tvLabel.setText(label);
		mFormView.addView(tvLabel);
		String colors[] = { "-" , "1", "X", "2", "1 - X", "1 - 2", "X - 2", "1 - X - 2" };
		if (spinnerPromoGoal) {
			colors = null;
			colors = new String[]{ "-" , "1", "2", "3+" };
		} 
		
		// Selection of the spinner
		Spinner spinnerView = new Spinner(getActivity());
		// Application of the Array to the Spinner
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item, colors);
		spinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down vieww
		spinnerView.setAdapter(spinnerArrayAdapter);

		spinnerView.setId(id());
		mFormView.addView(spinnerView);

	}
	
	private void addFormField(String label, int type, boolean spinner) {
		TextView tvLabel = new TextView(getActivity());
		tvLabel.setLayoutParams(getDefaultParams(true));
		tvLabel.setText(label);
		mFormView.addView(tvLabel);

		if (spinner) {

			String colors[] = { "1", "X", "2", "1 - X", "1 - 2", "X - 2",
					"1 - X - 2" };

			// Selection of the spinner
			Spinner spinnerView = new Spinner(getActivity());

			// Application of the Array to the Spinner
			ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item, colors);
			spinnerArrayAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The
																								// drop
																								// down
																								// vieww
			spinnerView.setAdapter(spinnerArrayAdapter);

			spinnerView.setId(id());
			mFormView.addView(spinnerView);

		} else {
			EditText editView = new EditText(getActivity());
			editView.setLayoutParams(getDefaultParams(false));
			// setting an unique id is important in order to save the state
			// (content) of this view across screen configuration changes
			editView.setId(id());
			editView.setInputType(type);
			mFormView.addView(editView);
		}

	}

	private LayoutParams getDefaultParams(boolean isLabel) {
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		if (isLabel) {
			params.bottomMargin = 5;
			params.topMargin = 10;
		}
		return params;
	}
}
